<?php if ( ! defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );
/**
 * BackendPro
 *
 * A website backend system for developers for PHP 4.3.2 or newer
 *
 * @package         conlabz CMS
 * @author          Christian Schneider
 * @copyright       Copyright (c) 2009
 * @license        
 * @link            http://www.conlabz.de
 * @filesource
 */

// ---------------------------------------------------------------------------

/**
 * Blog
 *
 * A website, usually maintained with regular entries of commentary
 *
 * @package         conlabz CMS
 * @subpackage      Controllers
 */
class Orgunit extends User_Controller {
	function Orgunit( ) {
		parent::User_Controller( );
		
		$this->load->helper( 'form' );

		// Check for access permission
		// needs to be defined as a resource over the admin area
		//@todo: check( 'Orgunit' );

		// Load the validation library
		$this->load->library( 'validation' );

		// Load the orgunit model
		$this->load->model( 'orgunit_model' );
		$this->load->module_model( 'article', 'website_model' );
		
		// Load the blog module
		$this->lang->module_load( 'people', 'person' );
		$this->lang->module_load( 'people', 'personfunction' );
		$this->lang->module_load( 'people', 'orgunit' );

		// Set breadcrumb
		$this->bep_site->set_crumb( $this->lang->line( 'orgunit' ), 'people/user/orgunit' );
				
		log_message( 'debug', 'conlabz CMS : OrgUnit class loaded' );
	}	
	
	/**
	 * View OrgUnit
	 *
	 * @access public
	 */
	function index( ) {
		// Get orgunits
		$data['orgunit']   = $this->orgunit_model->getOrgUnits( );
		
		// Display Page
		$data['header'] = $this->lang->line( 'orgunit_overview' );
		$data['page']   = $this->config->item( 'backendpro_template_user' ) . "/view_orgunit";
		$data['menu'] = $this->website_model->getWebsiteUserMenu( );
		$data['module'] = 'people';
		
		$this->load->module_view( 'article', 'user/container', $data );
	}
		
	/**
	 * Get Blog Details
	 *
	 * Load blog detail values from the submitted form
	 *
	 * @access private
	 * @return array
	 */
	function _get_orgunit_details( ) {
		$data['id']    	     	= $this->input->post( 'id'          );
		$data['name'] 	     	= $this->input->post( 'name'        );
		$data['description'] 	= $this->input->post( 'description' );
		$data['email']      	= $this->input->post( 'email'      );
		$data['category_id']    = $this->input->post( 'category_id'   );
	
		return $data;
	}
	
	/**
	 * Display Blog Form
	 *
	 * @access public
	 * @param integer $id Blog ID
	 */
	function form( $id = NULL ) {
		// VALIDATION FIELDS
		$fields['id']          = "ID";
		$fields['name']        = $this->lang->line( 'orgunit_name'           );
		$fields['description'] = $this->lang->line( 'orgunit_description'     );
		$fields['email']       = $this->lang->line( 'orgunit_email' );
		$fields['category_id']    = $this->lang->line( 'orgunit_category_id'      );
		
		$this->validation->set_fields( $fields );

		// Setup validation rules
		if( is_null( $id ) ) {
			// Use create website rules (make sure no-one has the same short name)
			$rules['name']        = "trim|required";
			$rules['description'] = "trim|required";
		} else {                                  
			// Use edit website rules (make sure no-one other than the current user has the same short name)
			$rules['name']        = "trim|required";
			$rules['description'] = "trim|required";
		}
		
		// Setup form default values
		if( !is_null( $id ) AND !$this->input->post( 'submit' ) ) {
			// Modify form, first load
			$orgunit = $this->orgunit_model->getOrgUnits( array( 'orgunit.id' => $id ) );
			$orgunit = $orgunit->row_array( );
			$this->validation->set_default_value( $orgunit );
		} elseif( is_null( $id ) AND !$this->input->post( 'submit' ) ) {
			// Create form, first load
			$this->validation->set_default_value( 'category', $this->preference->item( 'default_orgunit_category' ) );
		} elseif( $this->input->post( 'submit' ) ) {
			// Form submitted, check rules
			$this->validation->set_rules( $rules );
		}

		// RUN
		if( $this->validation->run( ) === FALSE) {
			// Construct Category dropdown
			$website = " AND website_id = " . $this->session->userdata( 'website_id' );
			$this->load->module_model( 'article', 'category_model' );
			$data['category'] = $this->category_model->buildCategoryDropdown( 'category', 'id', $website );
			
			$this->validation->output_errors( );
			$data['fckeditor'] = array(
              	'name'        => 'description',
              	'id'          => 'description',
              	'toolbarset'  => 'Default',
              	'basepath'    => DEVPATH.'assets/plugins/fckeditor/',
              	'width'       => '60%',
              	'height'      => '300',
				'config'	  => '/template/'.$this->session->userdata( 'template' ).'/js/fckeditor.js'
    		);
			
			$this->validation->output_errors( );
			$data['header'] = (is_null( $id ) ? $this->lang->line( 'create_orgunit' ) : $this->lang->line( 'edit_orgunit' ) );
			$this->bep_site->set_crumb( $data['header'], 'people/user/orgunit/form/'.$id );
			$data['page']   = $this->config->item( 'backendpro_template_user' ) . "/form_orgunit";
			$data['menu'] = $this->website_model->getWebsiteUserMenu( );
			$data['module'] = 'people';
			$this->load->module_view( 'article', 'user/container', $data );
		} else {
			// Save form
			if( is_null( $id ) ) {
				// CREATE
				// Fetch form values
				$orgunit = $this->_get_orgunit_details( );
				
				$this->db->trans_start( );
				$orgunit['orgunit_id'] = $this->orgunit_model->insert( $orgunit );
				
				if( $this->db->trans_status( ) === TRUE ) {
					$this->db->trans_commit( );
					flashMsg( 'success', sprintf( $this->lang->line( 'userlib_user_saved' ), $orgunit['name'] ) );
				} else {
					$this->db->trans_rollback( );
					flashMsg( 'error', sprintf( $this->lang->line( 'backendpro_action_failed' ), $this->lang->line( 'userlib_create_orgunit' ) ) );
				}
				redirect( 'people/user/orgunit' );
			} else {
				// SAVE
				$orgunit = $this->_get_orgunit_details( );
				
				$this->db->trans_start( );
				$this->orgunit_model->update( $orgunit, $orgunit['id'], 'orgunit' );

				if( $this->db->trans_status( ) === TRUE ) {
					$this->db->trans_commit( );
					flashMsg( 'success', sprintf( $this->lang->line( 'userlib_user_saved' ), $orgunit['name'] ) );
				} else {
					$this->db->trans_rollback( );
					flashMsg( 'error', sprintf( $this->lang->line( 'backendpro_action_failed' ), $this->lang->line( 'userlib_edit_orgunit' ) ) );
				}
				redirect( 'people/user/orgunit' );
			}
		}
	} // form( );
	
	/**
	 * Delete
	 *
	 * Delete the selected blog from the system
	 *
	 * @access public
	 */
	function delete( ) {
		if( FALSE === ( $selected = $this->input->post( 'select' ) ) ) {
			redirect( 'people/user/orgunit', 'location' );
		}

		foreach( $selected as $orgunit ) {
			$this->db->delete( 'orgunit', array( 'id' => $orgunit ) );
		}

		flashMsg( 'success', $this->lang->line( 'blog_deleted' ) );
		redirect( 'people/user/orgunit', 'location' );
	}
	
}
/* End of file orgunit.php */
/* Location: ./modules/people/controllers/user/orgunit.php */